import { BoardSpace, Property, SpaceType, PropertyType, BoardTheme } from '../types';
import { getDefaultTheme } from '../data/themes';

export class BoardGenerator {
  /**
   * 生成完整的游戏棋盘
   */
  generateBoard(theme?: BoardTheme, seed?: number): BoardSpace[] {
    const selectedTheme = theme || getDefaultTheme();
    const board: BoardSpace[] = [];
    
    // 生成地块
    const properties = this.generateProperties(selectedTheme);
    
    // 创建40个位置
    for (let i = 0; i < 40; i++) {
      board.push(this.createBoardSpace(i, properties, selectedTheme));
    }
    
    return board;
  }

  /**
   * 创建单个棋盘空间
   */
  private createBoardSpace(position: number, properties: Property[], theme: BoardTheme): BoardSpace {
    // 特殊位置
    if (position === 0) {
      return {
        position,
        type: SpaceType.GO,
        name: theme.specialSpaceNames.go,
      };
    }
    
    if (position === 10) {
      return {
        position,
        type: SpaceType.JAIL,
        name: theme.specialSpaceNames.jail,
      };
    }
    
    if (position === 20) {
      return {
        position,
        type: SpaceType.FREE_PARKING,
        name: theme.specialSpaceNames.freeParking,
      };
    }
    
    if (position === 30) {
      return {
        position,
        type: SpaceType.GO_TO_JAIL,
        name: theme.specialSpaceNames.goToJail,
      };
    }
    
    // 机会卡位置 (2, 7, 17, 22, 33, 36)
    if ([2, 7, 17, 22, 33, 36].includes(position)) {
      return {
        position,
        type: SpaceType.CHANCE,
        name: '机会',
      };
    }
    
    // 社区卡位置 (3, 12, 18, 28, 35)
    if ([3, 12, 18, 28, 35].includes(position)) {
      return {
        position,
        type: SpaceType.COMMUNITY,
        name: '社区宝箱',
      };
    }
    
    // 税收位置 (4, 38)
    if ([4, 38].includes(position)) {
      return {
        position,
        type: SpaceType.TAX,
        name: position === 4 ? '所得税' : '奢侈税',
      };
    }
    
    // 其余位置为地块
    const property = properties.find(p => p.position === position);
    if (property) {
      return {
        position,
        type: SpaceType.PROPERTY,
        name: property.name,
        property,
      };
    }
    
    // 默认返回空地块
    return {
      position,
      type: SpaceType.PROPERTY,
      name: `位置${position}`,
    };
  }

  /**
   * 生成所有地块
   */
  generateProperties(theme: BoardTheme): Property[] {
    const properties: Property[] = [];
    const colorGroupKeys = Object.keys(theme.colorGroups);
    
    // 定义每个颜色组的位置
    const colorGroupPositions: { [key: string]: number[] } = {
      [colorGroupKeys[0]]: [1, 3],           // 棕色/青铜 - 2个
      [colorGroupKeys[1]]: [6, 8, 9],        // 浅蓝/银色 - 3个
      [colorGroupKeys[2]]: [11, 13, 14],     // 粉色/金色 - 3个
      [colorGroupKeys[3]]: [16, 18, 19],     // 橙色/钛合金 - 3个
      [colorGroupKeys[4]]: [21, 23, 24],     // 红色/霓虹蓝 - 3个
      [colorGroupKeys[5]]: [26, 27, 29],     // 黄色/等离子紫 - 3个
      [colorGroupKeys[6]]: [31, 32, 34],     // 绿色/激光红 - 3个
      [colorGroupKeys[7]]: [37, 39],         // 深蓝/量子绿 - 2个
    };
    
    // 铁路位置 (5, 15, 25, 35)
    const railroadPositions = [5, 15, 25, 35];
    
    // 公用事业位置 (12, 28)
    const utilityPositions = [12, 28];
    
    let propertyNameIndex = 0;
    
    // 生成颜色组地块
    colorGroupKeys.forEach((colorGroup, groupIndex) => {
      const positions = colorGroupPositions[colorGroup];
      const basePrice = 60 + groupIndex * 40;
      const baseRent = 2 + groupIndex * 2;
      
      positions.forEach((position, posIndex) => {
        properties.push({
          id: `property-${position}`,
          name: theme.propertyNames[propertyNameIndex++] || `地块${position}`,
          position,
          type: PropertyType.STREET,
          price: basePrice + posIndex * 20,
          rent: [
            baseRent,
            baseRent * 5,
            baseRent * 15,
            baseRent * 45,
            baseRent * 80,
            baseRent * 120,
          ],
          colorGroup,
          owner: null,
          level: 0,
          mortgaged: false,
          upgradeCost: (basePrice + posIndex * 20) / 2,
        });
      });
    });
    
    // 生成铁路
    railroadPositions.forEach((position, index) => {
      if (position !== 35) { // 35已经被社区卡占用
        properties.push({
          id: `railroad-${position}`,
          name: `${['东', '南', '西', '北'][index]}站`,
          position,
          type: PropertyType.RAILROAD,
          price: 200,
          rent: [25, 50, 100, 200],
          colorGroup: '铁路',
          owner: null,
          level: 0,
          mortgaged: false,
          upgradeCost: 0,
        });
      }
    });
    
    // 生成公用事业
    utilityPositions.forEach((position, index) => {
      if (position !== 12 && position !== 28) { // 这些位置被其他占用
        properties.push({
          id: `utility-${position}`,
          name: ['电力公司', '水务公司'][index],
          position,
          type: PropertyType.UTILITY,
          price: 150,
          rent: [4, 10], // 乘以骰子点数
          colorGroup: '公用事业',
          owner: null,
          level: 0,
          mortgaged: false,
          upgradeCost: 0,
        });
      }
    });
    
    return properties;
  }

  /**
   * 分配颜色组
   */
  assignColorGroups(properties: Property[]): void {
    // 颜色组已在生成时分配
    // 此方法保留用于未来可能的重新分配逻辑
  }

  /**
   * 放置特殊空间
   */
  placeSpecialSpaces(board: BoardSpace[]): void {
    // 特殊空间已在createBoardSpace中放置
    // 此方法保留用于未来可能的额外特殊空间逻辑
  }
}

// 导出单例实例
export const boardGenerator = new BoardGenerator();
